package com.lemon.cloud.mp.web;

import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.mp.config.WxMpInitConfigService;
import com.lemon.cloud.security.annotation.Inner;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@AllArgsConstructor
@RestController
@RequestMapping("/wx/jsApi/{appId}")
@Inner(value = false)
@Slf4j
public class WxJsapiController {

    @GetMapping("/getJsApiTicket")
    public ResultMsg<String> getJsApiTicket(@PathVariable("appId") String appId) {
        try {
            final WxMpService wxService = WxMpInitConfigService.getMpServices().get(appId);
            return ResultMsg.resultSuccess(wxService.getJsapiTicket(true));
        }catch (WxErrorException exception){
            log.error("getJsApiTicket异常，{}",exception.getLocalizedMessage());
        }
        return ResultMsg.resultFail("微信接口异常!");
    }
}
